from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from django.core.urlresolvers import reverse
from django.db.models import Q
# Create your views here.
from .. import models
from .viewsIndex import uploads
from .viewsIndex import pages
from django.contrib.auth.decorators import permission_required


@permission_required('myadmin.insert_types',raise_exception = True)
# 商品分类 添加
def catesadd(request):
    if request.method == 'GET':
        # 获取所有的商品分类
        data = models.Cates.objects.extra(select = {'paths':'concat(path,id)'}).order_by('paths')
        # 分配数据
        context = {'cateslist':data}
        # 加载模板
        return render(request,'myadmin/cates/add.html',context)
    elif request.method == 'POST':
        try:
            # 执行添加
            ob = models.Cates()
            ob.name = request.POST.get('name')
            ob.pid = request.POST.get('pid')
            # 判断是否为顶级分类
            if ob.pid == '0':
                ob.path = '0,'
            else:
                # 获取当前所选的分类的path
                fob = models.Cates.objects.get(id=ob.pid)
                #父级path＋父级id(自己的pid)
                ob.path = fob.path+ob.pid+','
            ob.save()
            return HttpResponse('<script>alert("添加成功");location.href="'+reverse('myadmin_cates_index')+'"</script>')
        except:
            return HttpResponse('<script>alert("添加失败");location.href="'+reverse('myadmin_cates_add')+'"</script>')


@permission_required('myadmin.show_types',raise_exception = True)
# 商品分类 列表
def catesindex(request):

    # 获取所有的商品分类
    # data = models.Cates.objects.all()
    # select *,concat(path,id) as paths from myadmin_cates order by paths;
    # 模型提供 extra
    #ob = Types.objects.extra(select = {'paths':'concat(path,id)'}).order_by('paths')
    #等同于
    #select *,concat(path,id) as paths from types order by paths;

    data = models.Cates.objects.extra(select = {'paths':'concat(path,id)'}).order_by('paths').exclude(isDelete=1)#排序
    
    keywords = request.GET.get('keywords')
    
    if keywords :
        data = data.filter(name__contains=keywords)
    #分页函数调用       
    cateslist = pages(data,request)
    #分配数据
    context = {'cateslist':cateslist}
    # 加载模板
    return render(request,'myadmin/cates/index.html',context)


@permission_required('myadmin.del_types',raise_exception = True)
#商品分类删除 逻辑删除
def catesdel(request):
    # 分类id
    cid = request.GET.get('cid')
    # 判断当前类下是否还有子类
    cnum = models.Cates.objects.filter(pid=cid).exclude(isDelete=1).count()
    if cnum :
        # 有子类,不能删除
        return JsonResponse({'error':1,'msg':'当前类下有子类,不能删除'})

    # 再确认当前类下是否右商品发布
    gnum = models.Cates.objects.get(id=cid).goods_set.all().exclude(status=2).count()
    if gnum :
        # 有子类,不能删除
        return JsonResponse({'error':1,'msg':'当前类下有商品,不能删除'})
    # 获取分类对象
    ob = models.Cates.objects.get(id=cid)
    ob.isDelete = True
    ob.save()
    return JsonResponse({'error':0,'msg':'删除成功'})

@permission_required('myadmin.del_types',raise_exception = True)
# 商品分类逻辑删除 列表
def catesdeletelist(request):

    data = models.Cates.objects.extra(select = {'paths':'concat(path,id)'}).order_by('paths').exclude(isDelete=0)#排序
    
    keywords = request.GET.get('keywords')
    
    if keywords :
        data = data.filter(name__contains=keywords)

    #分页函数调用       
    cateslist = pages(data,request)
    #分配数据
    context = {'cateslist':cateslist}
    # 加载模板
    return render(request,'myadmin/cates/deletelist.html',context)


@permission_required('myadmin.del_types',raise_exception = True)
# 商品分类分类 删除
def catesdelete(request):
    # 分类id
    cid = request.GET.get('cid')

    # 判断当前类下是否还有子类
    num = models.Cates.objects.filter(pid=cid).count()
    if num :
        # 有子类,不能删除
        return JsonResponse({'error':1,'msg':'当前类下有子类,不能删除'})

    # 再确认当前类下是否右商品发布

    # 获取分类对象
    ob = models.Cates.objects.get(id=cid)
    ob.delete()
    return JsonResponse({'error':0,'msg':'删除成功'})


@permission_required('myadmin.edit_types',raise_exception = True)
# 分类的修改
def catesedit(request):
    try:
        # 根据cid获取分类对象
        ob = models.Cates.objects.get(id=request.GET.get('cid'))
        ob.name = request.GET.get('name')
        ob.save()
        return JsonResponse({'error':0,'msg':'更新成功'})
    except:
        return JsonResponse({'error':1,'msg':'更新失败'})

#后台还原商品分类
def catesopen(request,pid,cid):
    try:
        # 判断当前类下是否还有父类
        num = models.Cates.objects.exclude(isDelete=1).filter(id=pid).count()
        print(num,pid,cid)
        if pid == "0":
            ob = models.Cates.objects.get(id=cid)
            ob.isDelete = 0
            ob.save()
            return HttpResponse('<script>alert("还原成功");location.href="'+reverse('myadmin_cates_index')+'"</script>')
        elif num: 
            ob = models.Cates.objects.get(id=cid)
            ob.isDelete = 0
            ob.save()   
            return HttpResponse('<script>alert("还原成功");location.href="'+reverse('myadmin_cates_index')+'"</script>')
        else:
            return HttpResponse('<script>alert("还原失败,请先还原上一级");location.href="'+reverse('myadmin_cates_deletelist')+'"</script>') 
    except:
        return HttpResponse('<script>alert("还原失败");location.href="'+reverse('myadmin_cates_deletelist')+'"</script>') 
